package com.lsh.payment.core.service.payment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.payment.api.model.basevo.ExceptionStatus;
import com.lsh.payment.api.model.payment.PaymentRequest;
import com.lsh.payment.core.exception.BusinessException;
import com.lsh.payment.core.model.payenum.*;
import com.lsh.payment.core.model.payment.PayDeal;
import com.lsh.payment.core.util.Idwork.RandomUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Project Name: lsh-payment
 *
 * @author peter
 * @date 16/11/27
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
public class PayFactory {

    /**
     * 创建支付流水记录
     *
     * @param paymentRequest
     * @return
     */
    public static PayDeal create(PaymentRequest paymentRequest) {

        PayDeal payDeal = new PayDeal();
        payDeal.setTradeId(paymentRequest.getTrade_id());
        payDeal.setVenderId(paymentRequest.getVenderId());
        if (paymentRequest.getTrade_module().equals(TradeModule.ORDER.getName())) {
            payDeal.setTradeModule(TradeModule.ORDER.getName());
        } else if (paymentRequest.getTrade_module().equals(TradeModule.BILL.getName())) {
            payDeal.setTradeModule(TradeModule.BILL.getName());
        } else if (paymentRequest.getTrade_module().equals(TradeModule.RECYCLE.getName())) {
            payDeal.setTradeModule(TradeModule.RECYCLE.getName());
        } else if (paymentRequest.getTrade_module().equals(TradeModule.RECHARGE.getName())) {
            payDeal.setTradeModule(TradeModule.RECHARGE.getName());
        } else {
            throw new BusinessException(ExceptionStatus.E2001002.getCode(), ExceptionStatus.E2001002.getMessage());
        }
//        支付流水号,支付订单号(第三版)支付平台订单号
        String payId = RandomUtil.snowFlakeId();

        payDeal.setPayId(payId);
        payDeal.setPayPaymentNo(payId);

        if (paymentRequest.getPay_way().equals(PayWay.IOS.getName())) {
            payDeal.setPayWay(PayWay.IOS.getName());
        } else if (paymentRequest.getPay_way().equals(PayWay.ANDROID.getName())) {
            payDeal.setPayWay(PayWay.ANDROID.getName());
        } else if (paymentRequest.getPay_way().equals(PayWay.H5.getName())) {
            payDeal.setPayWay(PayWay.H5.getName());
        } else if (paymentRequest.getPay_way().equals(PayWay.WINDOWS.getName())) {
            payDeal.setPayWay(PayWay.WINDOWS.getName());
        } else if (paymentRequest.getPay_way().equals(PayWay.SM.getName())) {
            payDeal.setPayWay(PayWay.SM.getName());
        } else if (paymentRequest.getPay_way().equals(PayWay.BSM.getName())) {
            payDeal.setPayWay(PayWay.BSM.getName());
        } else if (paymentRequest.getPay_way().equals(PayWay.XCX.getName())) {
            payDeal.setPayWay(PayWay.XCX.getName());
        } else {
            throw new BusinessException(ExceptionStatus.E2001002.getCode(), ExceptionStatus.E2001002.getMessage());
        }

        payDeal.setRequestAmount(new BigDecimal(paymentRequest.getRequest_amount()));

        payDeal.setPayType(paymentRequest.getPay_type());
        String tradeTypeCode = paymentRequest.getTrade_type();
        TradeType tradeType = TradeType.paseTradeTypeByCode(Integer.parseInt(tradeTypeCode));
        if (null == tradeType) {
            throw new BusinessException(ExceptionStatus.E2001002.getCode(), "tradeTypeCode : " + tradeTypeCode + ", " + ExceptionStatus.E2001002.getMessage());
        }
        payDeal.setPayChannel(tradeType.getChannelName());
        payDeal.setPayStatus(PayStatus.PAYING.getValue());
        payDeal.setOperateStatus(OperateStatus.PAYMENT_NO_CALLBACK.getCode());
        payDeal.setCreatedAt(new Date());
        payDeal.setUpdatedAt(new Date());
        //支付模块类型，1-主支付模块，2-副支付模块
        payDeal.setModuleType(ModubleType.BASE.getCode());
        //调用系统,默认1，商城
        if (paymentRequest.getSystem().compareTo(CallSystem.TMS.getCode()) == 0) {
            payDeal.setSystem(CallSystem.TMS.getCode());
        } else {
            payDeal.setSystem(CallSystem.YG.getCode());
        }

//        payDeal.setSystem(CallSystem.YG.getCode());

        //调用系统,默认1，商城
        if (paymentRequest.getSystem_channel() != null) {
            payDeal.setSystemChannel(paymentRequest.getSystem_channel());
        } else {
            payDeal.setSystemChannel(CallSystem.YG.getCode());
        }

        String ext = paymentRequest.getExt();
        JSONObject extJson = new JSONObject();
        if (StringUtils.isNotBlank(ext) && ext.length() > 10) {
            extJson.put("ext", JSON.parseObject(ext));
        }
        String uid = paymentRequest.getUid();
        if (StringUtils.isNotBlank(uid)) {
            extJson.put("uid", uid);
        }
        payDeal.setExt(extJson.toJSONString());
        payDeal.setLshNotifyUrl(paymentRequest.getNotify_url());
        payDeal.setTradeType(Integer.parseInt(paymentRequest.getTrade_type()));

        return payDeal;
    }

}
